/**
 * jims
 */
package com.jims.master.modules.nurse.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.doctor.entity.PatVisit;
import com.jims.master.modules.doctor.vo.TurnInVo;
import com.jims.master.modules.nurse.entity.BedRec;
import com.jims.master.modules.nurse.service.BedRecService;
import com.jims.master.modules.nurse.service.WardCirculationService;
import com.jims.master.modules.sys.entity.OrgDept;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.service.OrgDeptService;
import com.jims.master.modules.sys.service.OrgGroupVsDeptService;
import com.jims.master.modules.sys.service.OrgGroupVsUserService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 医生病历流转Controller
 *
 * @author CTQ
 * @version 2017年3月28日09:25:27
 */
@Controller
@RequestMapping(value = "${adminPath}/nurse/wardCirculation")
public class WardCirculationController extends BaseController {

    @Autowired
    private WardCirculationService wardCirculationService;
    @Autowired
    private OrgGroupVsDeptService orgGroupVsDeptService;
    @Autowired
    private OrgGroupVsUserService orgGroupVsUserService;
    @Autowired
    private OrgDeptService orgDeptService;
    @Autowired
    private BedRecService bedRecService;

    @ModelAttribute
    public PatVisit get(@RequestParam(required = false) String id) {
        PatVisit entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = wardCirculationService.get(id);
        }
        if (entity == null) {
            entity = new PatVisit();
        }
        return entity;
    }


    /**
     * @param turnInVo 传递参数
     * @return String   返回类型
     * @throws
     * @Title: turnInIndexWard
     * @Desciption: (加载移入病区病人页面)
     * @author CTQ
     * @date 2017-04-06 09:16:57
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "turnInIndexWard")
    public String turnInIndexWard(TurnInVo turnInVo, Model model) {

        model.addAttribute("turnInVo", turnInVo);
        return "modules/nurse/turnInIndexWard";
    }


    /**
     * @param turnInVo 传递参数
     * @return String    返回类型
     * @throws
     * @Title: waitWardPatientList
     * @Desciption: 查询所有待移入当前病区病人病历)
     * @author CTQ
     * @date 2017-04-06 09:16:57
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"waitWardPatientList", ""})
    public String waitDeptPatientList(TurnInVo turnInVo, Model model) {
        turnInVo.setOrgId(UserUtils.getOrgId());
        turnInVo.setDeptTransferedTo(UserUtils.getUser().getOrgDept().getId());
        List<TurnInVo> list = wardCirculationService.waitWardPatientList(turnInVo);
        model.addAttribute("list", list);
        model.addAttribute("turnInVo", turnInVo);
        return "modules/nurse/turnIn_newInWard";
    }

    /**
     * @param turnInVo 传递参数
     * @return String    返回类型
     * @throws
     * @Title: allWaitWardPatientList
     * @Desciption: 查全院所有待移入病区病人病历)
     * @author CTQ
     * @date 2017-04-06 09:16:57
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"allWaitWardPatientList", ""})
    public String allwaitDeptPatientList(TurnInVo turnInVo, Model model) {
        turnInVo.setOrgId(UserUtils.getOrgId());
        List<TurnInVo> list = wardCirculationService.waitWardPatientList(turnInVo);
        model.addAttribute("list", list);
        model.addAttribute("turnInVo", turnInVo);
        return "modules/nurse/turnIn_turnInWard";
    }


    /**
     * @param turnInVo 传递参数
     * @return String    返回类型
     * @throws
     * @Title: findInfoByParam
     * @Desciption: (选择病人回填表单数据)
     * @author CTQ
     * @date 2017-04-06 09:16:57
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "findInfoByParam")
    public String findInfoByParam(TurnInVo turnInVo, String type, Model model) {
        User user = UserUtils.getUser();
        model.addAttribute("user", user);
        if (StringUtils.isNotBlank(type)) {
            if (StringUtils.isNotBlank(turnInVo.getBedRecId())) {
                BedRec bedRec = bedRecService.get(turnInVo.getBedRecId());
                turnInVo.setBedNo(String.valueOf(bedRec.getBedNo()));
                turnInVo.setBedLabel(bedRec.getBedLabel());
            }
            model.addAttribute("turnInVo", turnInVo);
            return "modules/nurse/turnIn_form";
        } else {
            turnInVo.setOrgId(user.getOrgId());
           // turnInVo.setDeptTransferedTo(user.getOrgDept()!=null?user.getOrgDept().getId():"");
            turnInVo.setSituationDate(new Date());
            TurnInVo vo = wardCirculationService.findInfoByParam(turnInVo);
            OrgDept dept = orgDeptService.get(user.getOrgDept().getId());

            if (vo != null) {
                if (StringUtils.isNotBlank(turnInVo.getBedRecId())) {
                    BedRec bedRec = bedRecService.get(turnInVo.getBedRecId());
                    vo.setBedRecId(turnInVo.getBedRecId());
                    vo.setBedNo(String.valueOf(bedRec.getBedNo()));
                    vo.setBedLabel(bedRec.getBedLabel());
                }
                vo.setWard(dept);
            } else {
                vo = new TurnInVo();
            }
            model.addAttribute("turnInVo", vo);
            /*根据科室查询科室下医生*/
            List<User> result = UserUtils.getRoleUserList(dept.getId());
            model.addAttribute("users", result);
            return "modules/nurse/turnIn_form";
        }
    }

    /**
     * @param turnInVo 传递参数
     * @return String    返回类型
     * @throws
     * @Title: findInfoByParam
     * @Desciption: (根据病人身份证号查询病人)
     * @author CTQ
     * @date 2017-07-25 10:23:11
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "validIdCard")
    @ResponseBody
    public StringData validIdCard(TurnInVo turnInVo) {
        StringData stringData = new StringData();
        turnInVo.setOrgId(UserUtils.getOrgId());
        turnInVo.setSituationDate(new Date());
        TurnInVo vo = wardCirculationService.findInfoByParam(turnInVo);
        if (vo != null) {
            stringData.setCode("success");
        } else {
            stringData.setCode("error");
        }
        return stringData;
    }

    /**
     * @param turnInVo 传递参数
     * @return StringData    返回类型
     * @throws
     * @Title: turnIn
     * @Desciption: (医生新建/移入病人病历)
     * @author CTQ
     * @date 2017-03-28
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "turnIn")
    @ResponseBody
    public StringData turnIn(TurnInVo turnInVo) {
        StringData data = new StringData();
        String i = wardCirculationService.turnInPatient(turnInVo);
        if (StringUtils.isNotBlank(i) && i.equals("0")) {
            data.setCode("success");
            data.setData("移入病人入病区成功");
        } else {
            data.setCode("error");
            data.setData("移入病人入病区失败");
        }
        return data;
    }

    /**
     * @param turnInVo 传递参数
     * @return String   返回类型
     * @throws
     * @Title: turnOutDept
     * @Desciption: (加载移出病历页面)
     * @author CTQ
     * @date 2017-03-31
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "cancelTurnInWard")
    public String cancelTurnInWard(TurnInVo turnInVo, Model model) {
        model.addAttribute("turnInVo", turnInVo);
        return "modules/nurse/cancel_turnInIndex";
    }

    /**
     * @param turnInVo 传递参数
     * @return String   返回类型
     * @throws
     * @Title: findWaitTurnOutPatientList
     * @Desciption: (查询所有待出病人列表)
     * @author CTQ
     * @date 2017-03-31
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"findWaitCancelTurnInPatientList", ""})
    public String findWaitCancelTurnInPatientList(TurnInVo turnInVo, Model model) {
        turnInVo.setOrgId(UserUtils.getOrgId());
        turnInVo.setWardCode(UserUtils.getUser().getOrgDept()!=null?UserUtils.getUser().getOrgDept().getId():"");
        List<TurnInVo> list = wardCirculationService.findWaitTurnOutPatientList(turnInVo);
        model.addAttribute("list", list);
        model.addAttribute("turnInVo", turnInVo);
        return "modules/nurse/cancel_turnInList";
    }

    /**
     * @param turnInVo 传递参数
     * @return String    返回类型
     * @throws
     * @Title: findInfoByCancelTurnInParam
     * @Desciption: (查询要取消移出病历病人信息)
     * @author CTQ
     * @date 2017-04-01
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "findInfoByCancelTurnInParam")
    public String findInfoByCancelTurnInParam(TurnInVo turnInVo, Model model) {
        User user = UserUtils.getUser();
        turnInVo.setOrgId(user.getOrgId());
        turnInVo.setDeptTransferedTo(user.getOrgDept().getId());
        turnInVo.setSituationDate(new Date());
        TurnInVo vo = wardCirculationService.findInfoByTurnOutParam(turnInVo);
        OrgDept dept = orgDeptService.get(user.getOrgDept().getId());
        if (vo != null) {
            vo.setWard(dept);
            vo.setTransferDate(new Date());
        } else {
            vo = new TurnInVo();
        }
        vo.setBedRecId(turnInVo.getBedRecId());
        model.addAttribute("turnInVo", vo);
        /*根据科室查询科室下医生*/
        List<User> result = UserUtils.getRoleUserList(dept.getId());
        model.addAttribute("users", result);
        return "modules/nurse/cancel_turnInForm";

    }

    /**
     * @param turnInVo 传递参数
     * @return StringData    返回类型
     * @throws
     * @Title: saveCancelTurnIn
     * @Desciption: (取消移入病历)
     * @author CTQ
     * @date 2017-04-01
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "saveCancelTurnIn")
    @ResponseBody
    public StringData saveCancelTurnIn(TurnInVo turnInVo) {

        return wardCirculationService.cancelTurnInPatient(turnInVo);
    }

    /**
     * @param turnInVo 传递参数
     * @return String   返回类型
     * @throws
     * @Title: turnOutWardIndex
     * @Desciption: (加载移出病历页面)
     * @author CTQ
     * @date 2017-03-31
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "turnOutWardIndex")
    public String turnOutWardIndex(TurnInVo turnInVo, Model model) {
        model.addAttribute("turnInVo", turnInVo);
        return "modules/nurse/turnOutIndex";
    }

    /**
     * @param turnInVo 传递参数
     * @return String   返回类型
     * @throws
     * @Title: findWaitTurnOutPatientList
     * @Desciption: (查询所有待出病人列表)
     * @author CTQ
     * @date 2017-03-31
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"findWaitTurnOutPatientList", ""})
    public String findWaitTurnOutPatientList(TurnInVo turnInVo, Model model) {
        turnInVo.setOrgId(UserUtils.getOrgId());
        turnInVo.setWardCode(UserUtils.getUser().getOrgDept().getId());
        List<TurnInVo> list = wardCirculationService.findWaitTurnOutPatientList(turnInVo);
        model.addAttribute("list", list);
        return "modules/nurse/turnOutList";
    }

    /**
     * @param turnInVo 传递参数
     * @return String   返回类型
     * @throws
     * @Title: findInfoByTurnOutParam
     * @Desciption: (根据参数查询要移出病历病人信息)
     * @author CTQ
     * @date 2017-03-31
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "findInfoByTurnOutParam")
    public String findInfoByTurnOutParam(TurnInVo turnInVo, Model model) {
        User user = UserUtils.getUser();
        turnInVo.setOrgId(user.getOrgId());
        turnInVo.setDeptTransferedTo(user.getOrgDept().getId());
        turnInVo.setSituationDate(new Date());
        TurnInVo vo = wardCirculationService.findInfoByTurnOutParam(turnInVo);
        OrgDept dept = orgDeptService.get(user.getOrgDept().getId());
        if (vo != null) {
            vo.setDept(dept);
            vo.setTransferDate(new Date());
        } else {
            vo = new TurnInVo();
        }
        vo.setBedRecId(turnInVo.getBedRecId());
        model.addAttribute("turnInVo", vo);
		/*根据科室查询科室下医生*/
        List<User> result = UserUtils.getRoleUserList(dept.getId());
        model.addAttribute("users", result);
        return "modules/nurse/turnOutForm";

    }

    /**
     * @param turnInVo 传递参数
     * @return StringData    返回类型
     * @throws
     * @Title: turnOutPatient
     * @Desciption: (移出病人病历)
     * @author CTQ
     * @date 2017-03-31
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "saveTurnOutPatient")
    @ResponseBody
    public StringData turnOutPatient(TurnInVo turnInVo) {
        return wardCirculationService.turnOutPatient(turnInVo);
    }

    /**
     * @param turnInVo 传递参数
     * @return String   返回类型
     * @throws
     * @Title: cancelTurnOutWardIndex
     * @Desciption: (加载取消移出病历页面)
     * @author CTQ
     * @date 2017-03-31
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "cancelTurnOutWardIndex")
    public String cancelTurnOutWardIndex(TurnInVo turnInVo, Model model) {
        model.addAttribute("turnInVo", turnInVo);
        return "modules/nurse/cancel_turnOutIndex";
    }

    /**
     * @param turnInVo 传递参数
     * @return String   返回类型
     * @throws
     * @Title: findWaitCancelPatientList
     * @Desciption: (查询所有待取消转出病人列表)
     * @author CTQ
     * @date 2017-03-31
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"findWaitCancelPatientList", ""})
    public String findWaitCancelPatientList(TurnInVo turnInVo, Model model) {
        turnInVo.setOrgId(UserUtils.getOrgId());
        turnInVo.setDeptTransferedFrom(UserUtils.getUser().getOrgDept().getId());
        List<TurnInVo> list = wardCirculationService.findWaitCancelPatientList(turnInVo);
        model.addAttribute("list", list);
        return "modules/nurse/cancel_turnOutList";
    }

    /**
     * @param turnInVo 传递参数
     * @return String   返回类型
     * @throws
     * @Title: findInfoByCancelParam
     * @Desciption: (根据参数查询病人信息)
     * @author CTQ
     * @date 2017-03-31
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "findInfoByCancelParam")
    public String findInfoByCancelParam(TurnInVo turnInVo, Model model) {
        User user = UserUtils.getUser();
        turnInVo.setOrgId(user.getOrgId());
        turnInVo.setDeptTransferedTo(user.getOrgDept().getId());
        turnInVo.setSituationDate(new Date());
        TurnInVo vo = wardCirculationService.findInfoByCancelParam(turnInVo);
        OrgDept dept = orgDeptService.get(user.getOrgDept().getId());
        vo.setDept(dept);
        vo.setLastAcctDateTim(new Date());
        model.addAttribute("turnInVo", vo);
		/*根据科室查询科室下医生*/
        List<User> result = UserUtils.getRoleUserList(dept.getId());
        model.addAttribute("users", result);
        return "modules/nurse/cancel_turnOutForm";

    }

    /**
     * @param turnInVo 传递参数
     * @return StringData    返回类型
     * @throws
     * @Title: cancleTurnOutPatient
     * @Desciption: (取消移出病人病历)
     * @author CTQ
     * @date 2017-03-31
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "saveCancleTurnOutPatient")
    @ResponseBody
    public StringData saveCancleTurnOutPatient(TurnInVo turnInVo) {
        StringData data = new StringData();
        String i = wardCirculationService.cancleTurnOutPatient(turnInVo);
        if (StringUtils.isNotBlank(i) && i.equals("0")) {
            data.setCode("success");
            data.setData("取消移出病人病历成功");
        } else {
            data.setCode("error");
            data.setData("取消移出病人病历失败");
        }
        return data;
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "leaveHospIndex")
    public String leaveHospIndex(TurnInVo turnInVo, Model model) {
        model.addAttribute("turnInVo", turnInVo);
        return "modules/nurse/leaveHospIndex";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = {"findWaitLeaveHospPatientList", ""})
    public String findWaitLeaveHospPatientList(TurnInVo turnInVo, Model model) {
        turnInVo.setOrgId(UserUtils.getOrgId());
        turnInVo.setWardCode(UserUtils.getUser().getOrgDept().getId());
        List<TurnInVo> list = wardCirculationService.findWaitLeaveHospPatientList(turnInVo);
        model.addAttribute("list", list);
        return "modules/nurse/leaveHospList";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "findInfoByLeaveHospParam")
    public String findInfoByLeaveHospParam(TurnInVo turnInVo, Model model) {
        User user = UserUtils.getUser();
        turnInVo.setOrgId(user.getOrgId());
        TurnInVo vo = wardCirculationService.findInfoByLeaveHospParam(turnInVo);
        OrgDept dept = orgDeptService.get(user.getOrgDept().getId());
        if (vo != null) {
            vo.setWardCode(dept.getId());
            vo.setTransferDate(new Date());
        } else {
            vo = new TurnInVo();
        }
        vo.setDischargeDate(new Date());
        model.addAttribute("turnInVo", vo);
        return "modules/nurse/leaveHospForm";

    }

    @RequiresPermissions("user")
    @RequestMapping(value = "saveLeaveHospPatient")
    @ResponseBody
    public StringData saveLeaveHospPatient(TurnInVo turnInVo) {
        return wardCirculationService.leaveHospPatient(turnInVo);
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "cancelLeaveHospIndex")
    public String cancelLeaveHospIndex(TurnInVo turnInVo, Model model) {
        model.addAttribute("turnInVo", turnInVo);
        return "modules/nurse/cancel_leaveHospIndex";
    }


    @RequiresPermissions("user")
    @RequestMapping(value = {"findWaitCancelLeaveHospPatientList", ""})
    public String findWaitCancelLeaveHospPatientList(TurnInVo turnInVo, HttpServletRequest request, HttpServletResponse response, Model model) {
        turnInVo.setOrgId(UserUtils.getOrgId());
        turnInVo.setWardCode(UserUtils.getUser().getOrgDept().getId());
        turnInVo.setSettledIndicator("1");
        Page<TurnInVo> page = wardCirculationService.findWaitCancelLeaveHospPatientPage(new Page<TurnInVo>(request, response), turnInVo);
        model.addAttribute("page", page);
        return "modules/nurse/cancel_leaveHospList";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "findInfoByCancelLeaveHospParam")
    public String findInfoByCancelLeaveHospParam(TurnInVo turnInVo, Model model) {
        User user = UserUtils.getUser();
        model.addAttribute("user", user);
        turnInVo.setOrgId(user.getOrgId());
        turnInVo.setWardCode(UserUtils.getUser().getOrgDept()!=null?UserUtils.getUser().getOrgDept().getId():"");
        turnInVo.setSettledIndicator("1");
        TurnInVo vo = wardCirculationService.findInfoByCancelLeaveHospParam(turnInVo);
        OrgDept dept = orgDeptService.get(user.getOrgDept().getId());
        if (vo != null) {
            vo.setWardCode(dept.getId());
        } else {
            vo = new TurnInVo();
        }
        vo.setLastAcctDateTim(new Date());
        model.addAttribute("turnInVo", vo);
        List<User> result = UserUtils.getRoleUserList(dept.getId());
        model.addAttribute("users", result);
        return "modules/nurse/cancel_leaveHospForm";

    }

    @RequiresPermissions("user")
    @RequestMapping(value = "saveCancelLeaveHospPatient")
    @ResponseBody
    public StringData saveCancelLeaveHospPatient(TurnInVo turnInVo) {
        return wardCirculationService.cancelLeaveHospPatient(turnInVo);
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "emptyBedList")
    public String emptyBedList(BedRec bedRec, Model model) {
        User user = UserUtils.getUser();
        bedRec.setWordCode(user.getOrgDept()!=null?user.getOrgDept().getId():"");
        bedRec.updateOrgId();
        bedRec.setBedStatus("1");
        List<BedRec> list = bedRecService.findList(bedRec);
        model.addAttribute("list",list);
        return "modules/nurse/emptyBedList";

    }

}